package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;

import java.util.List;

@Service
@Slf4j
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    AddressBookMapper addressBookMapper;
    /**
     * 新增一个地址
     *
     * @param addressBook
     */
    @Override
    public void add(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBookMapper.insert(addressBook);

    }

    /**
     * 查询所有的地址
     */
    @Override
    public List<AddressBook>  listAll() {
        List<AddressBook> list=  addressBookMapper.selectAll();
        return list;
    }

    /**
     * 查询默认地址
     *
     * @return
     */
    @Override
    public AddressBook selectDef() {
        AddressBook addressBook= addressBookMapper.selectDef();
        return addressBook;
    }

    /**
     * 根据id查询信息
     *
     * @param id
     * @return
     */
    @Override
    public AddressBook getById(Long id) {
        AddressBook addressBook= addressBookMapper.selectById(id);
        return addressBook;
    }

    /**
     * 设置默认的地址
     *
     * @param id
     */
    @Override
    public void setDefAddress(AddressBook addressBook) {
        addressBook.setIsDefault(1);
        addressBookMapper.update(addressBook);

    }

    /**
     * 更新操作
     *
     * @param addressBook
     */
    @Override
    public void update(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.update(addressBook);
    }

    /**
     * 根据id删除
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        addressBookMapper.delete(id);
    }


}